package ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

public class getMoneyFrame extends JFrame {
    private MyJText textMoney = new MyJText("请输入取款金额",50,100,this);
    Properties p = new Properties();

    public getMoneyFrame(){

        try {
            p.load(new FileReader("Housework0906/src/ATM/ATM.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }




        this.setLayout(null);


        this.setLayout(null);
        MyButton saveButton = new MyButton("取款",350,200,this);

        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setTitle("取款");


        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getMoney();
            }
        });
    }

    private void getMoney(){
        int have = Integer.parseInt(p.getProperty("money"));
        int take = Integer.parseInt(textMoney.getText());
        if (have<take){
            JOptionPane.showMessageDialog(null,"余额不足，无法取款");
            this.dispose();
            new ItemFrame();
        }
        have -= take;
        p.setProperty("money",have+"");
        try {
            p.store(new FileWriter("Housework0906/src/ATM/ATM.txt"), null);
        } catch (Exception e){
            System.out.println("文件不存在");
        }

        this.dispose();
        new AskFrame();
    }

}
